package com.nq.stock.service.impl;

import com.nq.common.ServerResponse;
import com.nq.common.entity.NewsList;
import com.nq.common.entity.info.NewsInfo;
import com.nq.dao.stock.StNewsMapper;
import com.nq.stock.entity.StNews;
import com.nq.stock.service.IStNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_news
 * @Author: jeecg-boot
 * @Date:   2024-06-05
 * @Version: V1.0
 */
@Service
public class StNewsServiceImpl extends ServiceImpl<StNewsMapper, StNews> implements IStNewsService {

    @Resource
    private StNewsMapper stNewsMapper;

    @Override
    public int addNews(NewsList news) {

        if (null == news) {
            return 0;
        }

        List<NewsInfo> data = news.getData();
        if (null == data || data.isEmpty()) {
            return 0;
        }

        ArrayList<StNews> list = new ArrayList<>();

        for (NewsInfo info : data) {
            StNews stNews = new StNews();
            BeanUtils.copyProperties(info, stNews);
            list.add(stNews);
        }

        this.saveOrUpdateBatch(list);
        return 1;
    }

    @Override
    public ServerResponse getStNewsDetail(String newId, HttpServletRequest request) {
        StNews stNews = stNewsMapper.selectById(newId);
        return ServerResponse.createBySuccess(stNews);
    }
}
